package com.tigase.serviceImpl;
/**
 * Created by tianfeng on 2017/7/21.
 */

import com.tigase.bean.TigaseGroup;
import com.tigase.bean.TigaseGroupMembers;
import com.tigase.service.GroupService;
import com.tigase.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author tianfeng
 * @create 2017-07-21 8:50
 **/
@Service
public class GroupServerImpl implements GroupService{
    @Autowired
    private JdbcTemplate JdbcTemplate;

    public int insertGroup(TigaseGroup group) {
        String sql="INSERT  INTO  t_groups (group_id,group_jid,group_name,group_create_time,update_time,creat_name ) VALUES (?,?,?,?,?,?)";
        String uuid= UUIDUtils.getUUID();
        int a=JdbcTemplate.update(sql,uuid,group.getGroupJid(),group.getGroupName(),new Date(),new Date(),group.getCreatName());
       return 1;
    }


    public List<TigaseGroup> selectGroupByUserId(String userId) {
        List<TigaseGroup> list=new LinkedList<>();
        String sql="SELECT  * from t_groups WHERE group_id in(\n" +
                "(SELECT group_id FROM t_group_members where user_id =\n" +
                "\t\t(SELECT user_id FROM t_user WHERE user_name= ? )))";
       List<Map<String, Object>> aa= JdbcTemplate.queryForList(sql,userId);
        if(aa.size()>0){
           for(int i=0;i<aa.size();i++){
//               TigaseGroups oGroup=new TigaseGroups();
//               String jid= (String) aa.get(i).get("group_jid");
//               String name= (String) aa.get(i).get("group_name");
//               Date creattime= (Date) aa.get(i).get("group_creat_time");
//               String creatName= (String) aa.get(i).get("creat_name");
//               oGroup.setGroupJid(jid);
//               oGroup.setCreatName(name);
//               oGroup.setGroupCreateTime(creattime);
//               oGroup.setCreatName(creatName);
//               list.add(oGroup);
           }
       }
        return list;
    }


    public  List<Map<String,String>> findGroupMenmber(String groupId) {
        String sql="Select user_name,user_nick from t_user where user_id in (\n" +
                "SELECT user_id from  t_group_members where t_group_members.group_id=(\n" +
                "SELECT group_id from t_groups where group_jid= ? )) ";
        List<Map<String, Object>> as= JdbcTemplate.queryForList(sql,groupId);
        List<Map<String,String>> list=new LinkedList<>();
        if(as.size()>0){
            for (int i=0;i<as.size();i++){
                Map<String,String> map=new LinkedHashMap<>();
                String name= (String) as.get(i).get("user_name");
                String ckk= (String) as.get(i).get("user_nick");
                map.put("userName",name);
                map.put("userNick",ckk);
                list.add(map);
            }
        }else{
            return list;
        }

        return list;
    }


    public  TigaseGroup getGroupJid(String jid) {
        TigaseGroup oGroup=new TigaseGroup();
        String  sql="SELECT  group_id,group_jid,group_name FROM  t_groups WHERE  group_jid = ? ";
        List<Map<String, Object>> a =JdbcTemplate.queryForList(sql,jid);
        if(a.size()>0){
            for (int i=0;i<a.size();i++){
               String sId= (String) a.get(i).get("group_id");
               String sJid= (String) a.get(i).get("group_jid");
               String sName= (String) a.get(i).get("group_name");
               oGroup.setGroupId(sId);
               oGroup.setGroupJid(sJid);
               oGroup.setGroupName(sName);
            }
        }else{
            return oGroup;
        }
        return oGroup;
    }


    public int delGroupMember(String userid) {
        return 0;
    }


    public int insertGroupMember(TigaseGroupMembers members) {
        String sql="INSERT  INTO  t_group_members (id,group_id,user_id,user_type,in_time) VALUES  (?,?,?,?,?)";
        if(members.getUserType()==0){
            members.setUserType(3);
        }
        String uuid= UUIDUtils.getUUID();
       int result= JdbcTemplate.update(sql,new Object[]{uuid,members.getGroupId(),members.getUserId(),members.getUserType(),new Date()});


        return result;
    }
}
